define(function(require, exports, module){
	var data = require("data");
	

	var EventProxy = require("eventproxy");
	var Loading = require("loading").Loading;

	var CateActionBar = require("cate-actionbar");

	function Cates(id, bookmarkManager) {
		this.bookmarkManager = bookmarkManager;

		this.container = $("#" + id);
		this.init();
		this.render();
	}

	Cates.prototype = $.extend(new EventProxy(), {
		init: function(){
			var _this = this;

			this.loading = new Loading({
				container : this.container
			});

			this.ul = $("<ul></ul>");

			this.container.empty();

			// 创建工具栏
			var actionbarContainer = $("<div class='cate-actionbar-container'></div>");
			this.container.append(actionbarContainer);
			this.actionbar = new CateActionBar(actionbarContainer);


			this.ul.css({
				"overflow" : "auto",
				"height" : this.container.height() - actionbarContainer.outerHeight()
			});

			this.actionbar.on("edit", function(){
				_this.ul.find("a.link.active").editnow(function(val, link, oldVal){
					var cateId = link.data("cateid");

					var fail = function(){
						alert('修改分类失败');
					};

					data.call("cate/rename", {
						"id" : cateId,
						"name" : val
					}).done(function(json){
						if (json.error || json.errors) {
							fail();
						} else {
							_this.emit("rename", cateId, val);
							_this.emit("change");
						}
					}).fail(fail);
				});
			});
			this.actionbar.on("remove", function(){
				var link = _this.ul.find("a.link.active");
				var cateId = link.data("cateid");
				if (window.confirm("此操作将会删除该分类及分类以下的书签")) {


					var li = link.parent();
					var nextLi = li.prev() || li.next();
					var nextCateId = nextLi.find("a").data("cateid");
					_this.selectCate(nextCateId);
					li.hide();

					var fail = function(){
						alert('删除分类失败');
						li.show();
						_this.selectCate(cateId);
					};					

					data.call("cate/remove", {
						"id" : cateId
					}).done(function(json){
						if (json.error || json.errors) {
							fail();
						} else {
							_this.emit("remove", cateId);
							
							_this.emit("change");
						}
					}).fail(fail);
				}
			});

			this.container.append(this.ul);

			this.ul.on("click", ".link", function(){
				var cateId = $(this).data("cateid");
				_this.selectCate(cateId);
			});

			this.ul.on("drop", "li", function(e){
				var bookmarkid = e.originalEvent.dataTransfer.getData("Text");
				var cateId = $(this).data("cateid");

				$(this).removeClass("ondrag");

				if (cateId == _this.currentCateId()) {
					return ;
				}

				e.originalEvent.dataTransfer.setData("Text", "success");

				var fail = function(message){
					alert(message || "移动书签失败");
					_this.onCateSelected(_this.currentCateId());
				};

				data.call("bookmark/change-cate", {
					"id" : bookmarkid,
					"cateId" : cateId,
				}).done(function(json){
					if (json.status != 0) {
						fail(JSON.stringify(json.errors||json.error));
					}
				}).fail(fail);
				_this.emit("bookmarkMoved", bookmarkid);
			});

			this.ul.on("dragover", "li", function(e){
				e.preventDefault();
			});

			this.ul.on("dragenter", "li", function(){
				$(this).addClass("ondrag");
			});

			this.ul.on("dragleave", "li", function(){
				$(this).removeClass("ondrag");
			});
		},
		currentCateId: function(){
			return this.ul.find("a.link.active").data("cateid");
		},
		render: function(){
			var _this = this;

			this.loading.start();

			data.call("cate/list").done(function(json){
				var cates = json.data;
				var lastCateId = localStorage.lastCateId || 0;

				// 如果之前没有选中过任何分类，就自动选中第一个
				if (cates.length > 0 && lastCateId == 0) {
					lastCateId = cates[0].id;
				}

				cates.forEach(function(cate, index){
					var cls = cate.id == lastCateId ? "class=\"link active\"" : "class=\"link\"";
					$("<li data-cateid='"+cate.id+"'><a href=\"javascript:;\" "+ cls + " id='cate_"+cate.id+"' data-cateid='"+cate.id+"'>" + cate.name + "</a></li>").appendTo(_this.ul);
				});

				_this.selectCate(lastCateId);
				_this.loading.stop();
			});
		},
		rending: function(){

		},
		// 一个分类被选中的事件
		onCateSelected: function(id){
			if (this.bookmarkManager ) {
				this.bookmarkManager.show(id);
			}

			this.emit("cateSelected", id);
		},
		// 选中一个分类
		selectCate: function(id){
			this.ul.find("a.link").removeClass("active");
			this.ul.find("#cate_" + id).addClass("active");
			this.onCateSelected(id);
			// this.lastCateId = id;

			localStorage.lastCateId = id;
		},
		stopLoading: function(){
			this.loading.stop();
		}
	});

	return Cates;
});